import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';

import '../../theme/kq_theme_manager.dart';
import '../button/kq_outline_button.dart';

/// 通用确认对话框
///
/// @author 周卓
///
/// 顶部标题 + 中间消息 + 底部2个按钮
class KqConfirmDialog extends AlertDialog {
  /// 标题
  final String? titleString;

  /// 消息
  final String msg;

  /// 消息对齐方式
  final TextAlign? msgTextAlign;

  /// 左边按钮文字，默认"取消"
  final String? leftBtnText;

  /// 左边按钮文字，默认"确定"
  final String? rightBtnText;

  /// 左边按钮回调
  final Function()? onLeftBtnTap;

  /// 右边按钮回调
  final Function()? onRightBtnTap;

  /// 标题文字颜色
  final Color? titleColor;

  /// 消息文字颜色
  final Color? msgColor;

  /// 左边按钮文字颜色
  final Color? leftBtnColor;

  /// 左边按钮背景颜色
  final Color? leftBtnBgColor;

  /// 左边按钮边框颜色
  final Color? leftBtnBorderColor;

  /// 右边按钮文字颜色
  final Color? rightBtnColor;

  /// 右边按钮背景颜色
  final Color? rightBtnBgColor;

  /// 右边按钮边框颜色
  final Color? rightBtnBorderColor;

  /// 标题文字大小
  final double? titleFontSize;

  /// 消息文字大小
  final double? msgFontSize;

  /// 按钮文字
  final double? btnFontSize;

  const KqConfirmDialog({
    super.key,
    this.titleString,
    required this.msg,
    this.msgTextAlign,
    this.leftBtnText,
    this.rightBtnText,
    this.titleColor,
    this.leftBtnColor,
    this.leftBtnBgColor,
    this.leftBtnBorderColor,
    this.rightBtnColor,
    this.rightBtnBgColor,
    this.rightBtnBorderColor,
    this.msgColor,
    this.titleFontSize,
    this.msgFontSize,
    this.btnFontSize,
    this.onLeftBtnTap,
    this.onRightBtnTap,
  });

  @override
  Widget build(BuildContext context) {
    List<Widget> children = <Widget>[];

    /// 上边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    /// 标题
    if (titleString != null) {
      children.add(Container(
        padding: EdgeInsets.symmetric(horizontal: 24.r),
        alignment: Alignment.center,
        child: Text(
          titleString!,
          style: TextStyle(
            fontSize: titleFontSize ?? 28.sp,
            color:
                KqThemeManager.instance.getConfig().dialogConfig.titleColor ??
                    KqThemeColors.textBlack85,
          ),
        ),
      ));
      children.add(SizedBox(
        width: 1.r,
        height: 24.r,
      ));
    }

    /// 消息
    children.add(LimitedBox(
      maxHeight: 880.r,
      child: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.symmetric(horizontal: 24.r),
          padding: EdgeInsets.symmetric(horizontal: 12.r),
          alignment: Alignment.center,
          // constraints: BoxConstraints(
          //   minHeight: 140.r,
          // ),
          child: Text(
            msg,
            textAlign: msgTextAlign ?? TextAlign.center,
            style: TextStyle(
                fontSize: msgFontSize ?? 24.sp,
                color: msgColor ?? KqThemeColors.text59),
          ),
        ),
      ),
    ));

    /// 下边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    Widget column = Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: children,
    );
    column = Container(
      constraints: BoxConstraints(
        minHeight: 188.r,
      ),
      child: column,
    );
    children = [];
    children.add(column);

    /// 底部按钮
    children.add(Padding(
      padding: EdgeInsets.symmetric(horizontal: 40.r, vertical: 24.r),
      child: Row(
        children: [
          Expanded(
              child: KqOutlineButton.normal(
            title: leftBtnText ?? KqIntl.currentResource.cancel,
            textColor: leftBtnColor ?? KqThemeColors.bgBlue,
            bgColor: leftBtnBgColor,
            borderColor: leftBtnBorderColor ?? KqThemeColors.bgBlue,
            verticalPadding: 22.r,
            onTap: (disabled) {
              if (onLeftBtnTap != null) {
                onLeftBtnTap!();
              }
            },
          )),
          SizedBox(
            width: 24.r,
          ),
          Expanded(
              child: KqOutlineButton(
            title: rightBtnText ?? KqIntl.currentResource.confirm,
            textColor: rightBtnColor ?? KqThemeColors.textWhite,
            bgColor: rightBtnBgColor ?? KqThemeColors.bgBlue,
            borderColor: rightBtnBorderColor ?? KqThemeColors.bgBlue,
            verticalPadding: 22.r,
            onTap: (disabled) {
              if (onRightBtnTap != null) {
                onRightBtnTap!();
              }
            },
          ))
        ],
      ),
    ));

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    return UnconstrainedBox(
        child: SizedBox(
            width: 880.r,
            child: Material(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(
                        KqThemeManager.instance
                                .getConfig()
                                .dialogConfig
                                .radius ??
                            8.r))),
                color:
                    KqThemeManager.instance.getConfig().dialogConfig.bgColor ??
                        KqThemeColors.bgWhite,
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(
                      KqThemeManager.instance.getConfig().dialogConfig.radius ??
                          8.r)),
                  child: dialogChild,
                ))));
  }
}
